/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sv.gob.mh.dgii.model.pojos.ruc;

import java.io.Serializable;
import java.util.Date;
import java.util.Random;
import javax.validation.constraints.NotNull;
import sv.gob.mh.dgii.common.Utils;
import sv.gob.mh.dgii.model.RcDomicilio;
import sv.gob.mh.dgii.model.TbDomicilio;
import sv.gob.mh.dgii.model.TbMunicipio;
import sv.gob.mh.dgii.model.custom.constraints.TbMunicipioNotEmpty;

/**
 *
 * @author hgonzalez
 */
public class Domicilio implements Serializable{
    
        private static String SPACE = " ";
        
        private String[] siglas = new String[]{
                    "",
                    "Calle","Av.","Diag.",
                    "Prolg.","Blvd","Carr.","km.","Polig.",
                    "Psj.","Block","Sda.","Plazuela",
                    "Col.","Res.",
                    "Urb.","Bo.","Repto.","Cton.","Crio.","Lotif.",
                    "Valle","Com.","Cond.","C. Urb.","Ctro. Com.",
                    "Aldea","Alameda","Edif.","#","Apto.","Local.",
                    "Lote"                    
        };
        
        private static final Integer CASA = 29;
	private static final Integer APARTAMENTO = 30;
	private static final Integer LOCAL = 31;
	private static final Integer LOTE = 32;
        
        private String[] valores = new String[33];
        
        @TbMunicipioNotEmpty
        private TbMunicipio tbMunicipio;
        
        @NotNull
        private TbDomicilio tbDomicilio;
        
        private String email;
        private String telefono;
        private String fax;
        private String complemento;
        
        //Scalle
        private String calle;
        private String av;
        private String diag;
        private String prolg;
        private String blvd;
        private String carr;
        private String km;
        private String polig;
        private String psj;
        private String block;
        private String sda;
        private String plazuela;
        
        //Scolonia
        private String col;
        private String res;
        private String urb;
        private String bo;
        private String repto;
        private String cton;
        private String crio;
        private String lotif;
        private String valle;
        private String com;
        private String cond;
        private String curb;
        private String ctrocom;
        private String aldea;
        private String alameda;
        private String edif;
        
        //nnumero y/o apto
        private String numeral;
        private String apto;
        private String local;
        private String lote;
        
        public Domicilio(){
            
        }
        
        public Domicilio(String tbDomicilio){
            TbDomicilio tbDom = new TbDomicilio();
            tbDom.setCdomicilio(tbDomicilio);
            this.tbDomicilio = tbDom;
        }
                
        
        public RcDomicilio toRcDomicilio(){
            setValores();
            
            RcDomicilio dom = new RcDomicilio();
            
            // domicilio
            dom.setTbDomicilio(getTbDomicilio());
            
            // municipio
            dom.setTbMunicipio(tbMunicipio);
		
            dom.setNtelefono(telefono);
            dom.setNfax(fax);
            dom.setScomplemento(complemento);
            dom.setSemail(email);
            /**
             * Fecha inicio se seteara posteriormente
             * esto con el proposito de poder determinar los nuevos
             * registros con los antiguos
             */
            //dom.setFidomicilio(new Date());
	
            dom.setScalle(getCalle2());
            dom.setScolonia(getColonia());
            dom.setNnumero(getNumero());
            dom.setNapto(getApto2());
	
            dom.setIrcDomicilio(new Random().nextLong());
            
            System.out.println("DOM " + dom);
            
            return dom;
        }
        
        
        private void setValores(){
            valores[1] = calle;
            valores[2] = av;
            valores[3] = diag;
            valores[4] = prolg;
            valores[5] = blvd;
            valores[6] = carr;
            valores[7] = km;
            valores[8] = polig;
            valores[9] = psj;
            valores[10] = block;
            valores[11] = sda;
            valores[12] = plazuela;
            
            valores[13] = col;
            valores[14] = res;
            valores[15] = urb;
            valores[16] = bo;
            valores[17] = repto;
            valores[18] = cton;
            valores[19] = crio;
            valores[20] = lotif;
            valores[21] = valle;
            valores[22] = com;
            valores[23] = cond;
            valores[24] = curb;
            valores[25] = ctrocom;
            valores[26] = aldea;
            valores[27] = alameda;
            valores[28] = edif;
            
            valores[29] = numeral;
            valores[30] = apto;
            valores[31] = local;
            valores[32] = lote;
        }
        
        protected String getColonia() {
            StringBuffer sb = new StringBuffer();
            for (int i = 13; i < 29; i++) {
		String valor = valores[i];
		String prefix = siglas[i];
                    if (!Utils.isNullOrEmpty(valor)) {
			sb.append(prefix);
                        sb.append(SPACE);
                        sb.append(valor);                        
                    }
            }
            return sb.toString();
	}
        
        protected String getCalle2() {
            StringBuffer sb = new StringBuffer();
            for (int i = 1; i < 13; i++) {
		String valor = valores[i];
		String prefix = siglas[i];
                    if (!Utils.isNullOrEmpty(valor)) {
			sb.append(prefix);
                        sb.append(SPACE);
                        sb.append(valor);                        
                    }
            }
            return sb.toString();
	}
        
        protected String getNumero() {
            
                StringBuffer sb = new StringBuffer();
                for (int i = 29; i < 33; i++) {
                    String valor = valores[i];
                    String prefix = siglas[i];
                        if (!Utils.isNullOrEmpty(valor) && i != APARTAMENTO && i!= LOCAL) {
                            sb.append(prefix);
                            sb.append(SPACE);
                            sb.append(valor);                        
                        }
                }
                return sb.toString();
	}
        
        protected String getApto2() {
            
                StringBuffer sb = new StringBuffer();
                for (int i = 29; i < 33; i++) {
                    String valor = valores[i];
                    String prefix = siglas[i];
                        if (!Utils.isNullOrEmpty(valor) && i != CASA && i!= LOTE) {
                            sb.append(prefix);
                            sb.append(SPACE);
                            sb.append(valor);                        
                        }
                }
                return sb.toString();
	}

    /**
     * @return the tbMunicipio
     */
    public TbMunicipio getTbMunicipio() {
        return tbMunicipio;
    }

    /**
     * @param tbMunicipio the tbMunicipio to set
     */
    public void setTbMunicipio(TbMunicipio tbMunicipio) {
        this.tbMunicipio = tbMunicipio;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the telefono
     */
    public String getTelefono() {
        return telefono;
    }

    /**
     * @param telefono the telefono to set
     */
    public void setTelefono(String telefono) {
        this.telefono = telefono;
    }

    /**
     * @return the fax
     */
    public String getFax() {
        return fax;
    }

    /**
     * @param fax the fax to set
     */
    public void setFax(String fax) {
        this.fax = fax;
    }

    /**
     * @return the calle
     */
    public String getCalle() {
        return calle;
    }

    /**
     * @param calle the calle to set
     */
    public void setCalle(String calle) {
        this.calle = calle;
    }

    /**
     * @return the av
     */
    public String getAv() {
        return av;
    }

    /**
     * @param av the av to set
     */
    public void setAv(String av) {
        this.av = av;
    }

    /**
     * @return the diag
     */
    public String getDiag() {
        return diag;
    }

    /**
     * @param diag the diag to set
     */
    public void setDiag(String diag) {
        this.diag = diag;
    }

    /**
     * @return the prolg
     */
    public String getProlg() {
        return prolg;
    }

    /**
     * @param prolg the prolg to set
     */
    public void setProlg(String prolg) {
        this.prolg = prolg;
    }

    /**
     * @return the blvd
     */
    public String getBlvd() {
        return blvd;
    }

    /**
     * @param blvd the blvd to set
     */
    public void setBlvd(String blvd) {
        this.blvd = blvd;
    }

    /**
     * @return the carr
     */
    public String getCarr() {
        return carr;
    }

    /**
     * @param carr the carr to set
     */
    public void setCarr(String carr) {
        this.carr = carr;
    }

    /**
     * @return the km
     */
    public String getKm() {
        return km;
    }

    /**
     * @param km the km to set
     */
    public void setKm(String km) {
        this.km = km;
    }

    /**
     * @return the polig
     */
    public String getPolig() {
        return polig;
    }

    /**
     * @param polig the polig to set
     */
    public void setPolig(String polig) {
        this.polig = polig;
    }

    /**
     * @return the psj
     */
    public String getPsj() {
        return psj;
    }

    /**
     * @param psj the psj to set
     */
    public void setPsj(String psj) {
        this.psj = psj;
    }

    /**
     * @return the block
     */
    public String getBlock() {
        return block;
    }

    /**
     * @param block the block to set
     */
    public void setBlock(String block) {
        this.block = block;
    }

    /**
     * @return the sda
     */
    public String getSda() {
        return sda;
    }

    /**
     * @param sda the sda to set
     */
    public void setSda(String sda) {
        this.sda = sda;
    }

    /**
     * @return the plazuela
     */
    public String getPlazuela() {
        return plazuela;
    }

    /**
     * @param plazuela the plazuela to set
     */
    public void setPlazuela(String plazuela) {
        this.plazuela = plazuela;
    }

    /**
     * @return the col
     */
    public String getCol() {
        return col;
    }

    /**
     * @param col the col to set
     */
    public void setCol(String col) {
        this.col = col;
    }

    /**
     * @return the res
     */
    public String getRes() {
        return res;
    }

    /**
     * @param res the res to set
     */
    public void setRes(String res) {
        this.res = res;
    }

    /**
     * @return the urb
     */
    public String getUrb() {
        return urb;
    }

    /**
     * @param urb the urb to set
     */
    public void setUrb(String urb) {
        this.urb = urb;
    }

    /**
     * @return the bo
     */
    public String getBo() {
        return bo;
    }

    /**
     * @param bo the bo to set
     */
    public void setBo(String bo) {
        this.bo = bo;
    }

    /**
     * @return the repto
     */
    public String getRepto() {
        return repto;
    }

    /**
     * @param repto the repto to set
     */
    public void setRepto(String repto) {
        this.repto = repto;
    }

    /**
     * @return the cton
     */
    public String getCton() {
        return cton;
    }

    /**
     * @param cton the cton to set
     */
    public void setCton(String cton) {
        this.cton = cton;
    }

    /**
     * @return the crio
     */
    public String getCrio() {
        return crio;
    }

    /**
     * @param crio the crio to set
     */
    public void setCrio(String crio) {
        this.crio = crio;
    }

    /**
     * @return the lotif
     */
    public String getLotif() {
        return lotif;
    }

    /**
     * @param lotif the lotif to set
     */
    public void setLotif(String lotif) {
        this.lotif = lotif;
    }

    /**
     * @return the valle
     */
    public String getValle() {
        return valle;
    }

    /**
     * @param valle the valle to set
     */
    public void setValle(String valle) {
        this.valle = valle;
    }

    /**
     * @return the com
     */
    public String getCom() {
        return com;
    }

    /**
     * @param com the com to set
     */
    public void setCom(String com) {
        this.com = com;
    }

    /**
     * @return the cond
     */
    public String getCond() {
        return cond;
    }

    /**
     * @param cond the cond to set
     */
    public void setCond(String cond) {
        this.cond = cond;
    }

    /**
     * @return the curb
     */
    public String getCurb() {
        return curb;
    }

    /**
     * @param curb the curb to set
     */
    public void setCurb(String curb) {
        this.curb = curb;
    }

    /**
     * @return the ctrocom
     */
    public String getCtrocom() {
        return ctrocom;
    }

    /**
     * @param ctrocom the ctrocom to set
     */
    public void setCtrocom(String ctrocom) {
        this.ctrocom = ctrocom;
    }

    /**
     * @return the aldea
     */
    public String getAldea() {
        return aldea;
    }

    /**
     * @param aldea the aldea to set
     */
    public void setAldea(String aldea) {
        this.aldea = aldea;
    }

    /**
     * @return the alameda
     */
    public String getAlameda() {
        return alameda;
    }

    /**
     * @param alameda the alameda to set
     */
    public void setAlameda(String alameda) {
        this.alameda = alameda;
    }

    /**
     * @return the edif
     */
    public String getEdif() {
        return edif;
    }

    /**
     * @param edif the edif to set
     */
    public void setEdif(String edif) {
        this.edif = edif;
    }

    /**
     * @return the numeral
     */
    public String getNumeral() {
        return numeral;
    }

    /**
     * @param numeral the numeral to set
     */
    public void setNumeral(String numeral) {
        this.numeral = numeral;
    }

    /**
     * @return the apto
     */
    public String getApto() {
        return apto;
    }

    /**
     * @param apto the apto to set
     */
    public void setApto(String apto) {
        this.apto = apto;
    }

    /**
     * @return the local
     */
    public String getLocal() {
        return local;
    }

    /**
     * @param local the local to set
     */
    public void setLocal(String local) {
        this.local = local;
    }

    /**
     * @return the lote
     */
    public String getLote() {
        return lote;
    }

    /**
     * @param lote the lote to set
     */
    public void setLote(String lote) {
        this.lote = lote;
    }
    
      /**
     * @return the tbDomicilio
     */
    public TbDomicilio getTbDomicilio() {
        return tbDomicilio;
    }

    /**
     * @param tbDomicilio the tbDomicilio to set
     */
    public void setTbDomicilio(TbDomicilio tbDomicilio) {
        this.tbDomicilio = tbDomicilio;
    }

    
    
    @Override
    public int hashCode() {
        int hash = 3;
        hash = 53 * hash + (this.tbMunicipio != null ? this.tbMunicipio.hashCode() : 0);
        hash = 53 * hash + (this.email != null ? this.email.hashCode() : 0);
        hash = 53 * hash + (this.telefono != null ? this.telefono.hashCode() : 0);
        hash = 53 * hash + (this.fax != null ? this.fax.hashCode() : 0);
        hash = 53 * hash + (this.calle != null ? this.calle.hashCode() : 0);
        hash = 53 * hash + (this.av != null ? this.av.hashCode() : 0);
        hash = 53 * hash + (this.diag != null ? this.diag.hashCode() : 0);
        hash = 53 * hash + (this.prolg != null ? this.prolg.hashCode() : 0);
        hash = 53 * hash + (this.blvd != null ? this.blvd.hashCode() : 0);
        hash = 53 * hash + (this.carr != null ? this.carr.hashCode() : 0);
        hash = 53 * hash + (this.km != null ? this.km.hashCode() : 0);
        hash = 53 * hash + (this.polig != null ? this.polig.hashCode() : 0);
        hash = 53 * hash + (this.psj != null ? this.psj.hashCode() : 0);
        hash = 53 * hash + (this.block != null ? this.block.hashCode() : 0);
        hash = 53 * hash + (this.sda != null ? this.sda.hashCode() : 0);
        hash = 53 * hash + (this.plazuela != null ? this.plazuela.hashCode() : 0);
        hash = 53 * hash + (this.col != null ? this.col.hashCode() : 0);
        hash = 53 * hash + (this.res != null ? this.res.hashCode() : 0);
        hash = 53 * hash + (this.urb != null ? this.urb.hashCode() : 0);
        hash = 53 * hash + (this.bo != null ? this.bo.hashCode() : 0);
        hash = 53 * hash + (this.repto != null ? this.repto.hashCode() : 0);
        hash = 53 * hash + (this.cton != null ? this.cton.hashCode() : 0);
        hash = 53 * hash + (this.crio != null ? this.crio.hashCode() : 0);
        hash = 53 * hash + (this.lotif != null ? this.lotif.hashCode() : 0);
        hash = 53 * hash + (this.valle != null ? this.valle.hashCode() : 0);
        hash = 53 * hash + (this.com != null ? this.com.hashCode() : 0);
        hash = 53 * hash + (this.cond != null ? this.cond.hashCode() : 0);
        hash = 53 * hash + (this.curb != null ? this.curb.hashCode() : 0);
        hash = 53 * hash + (this.ctrocom != null ? this.ctrocom.hashCode() : 0);
        hash = 53 * hash + (this.aldea != null ? this.aldea.hashCode() : 0);
        hash = 53 * hash + (this.alameda != null ? this.alameda.hashCode() : 0);
        hash = 53 * hash + (this.edif != null ? this.edif.hashCode() : 0);
        hash = 53 * hash + (this.numeral != null ? this.numeral.hashCode() : 0);
        hash = 53 * hash + (this.apto != null ? this.apto.hashCode() : 0);
        hash = 53 * hash + (this.local != null ? this.local.hashCode() : 0);
        hash = 53 * hash + (this.lote != null ? this.lote.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Domicilio other = (Domicilio) obj;
        if (this.tbMunicipio != other.tbMunicipio && (this.tbMunicipio == null || !this.tbMunicipio.equals(other.tbMunicipio))) {
            return false;
        }
        if ((this.email == null) ? (other.email != null) : !this.email.equals(other.email)) {
            return false;
        }
        if ((this.telefono == null) ? (other.telefono != null) : !this.telefono.equals(other.telefono)) {
            return false;
        }
        if ((this.fax == null) ? (other.fax != null) : !this.fax.equals(other.fax)) {
            return false;
        }
        if ((this.calle == null) ? (other.calle != null) : !this.calle.equals(other.calle)) {
            return false;
        }
        if ((this.av == null) ? (other.av != null) : !this.av.equals(other.av)) {
            return false;
        }
        if ((this.diag == null) ? (other.diag != null) : !this.diag.equals(other.diag)) {
            return false;
        }
        if ((this.prolg == null) ? (other.prolg != null) : !this.prolg.equals(other.prolg)) {
            return false;
        }
        if ((this.blvd == null) ? (other.blvd != null) : !this.blvd.equals(other.blvd)) {
            return false;
        }
        if ((this.carr == null) ? (other.carr != null) : !this.carr.equals(other.carr)) {
            return false;
        }
        if ((this.km == null) ? (other.km != null) : !this.km.equals(other.km)) {
            return false;
        }
        if ((this.polig == null) ? (other.polig != null) : !this.polig.equals(other.polig)) {
            return false;
        }
        if ((this.psj == null) ? (other.psj != null) : !this.psj.equals(other.psj)) {
            return false;
        }
        if ((this.block == null) ? (other.block != null) : !this.block.equals(other.block)) {
            return false;
        }
        if ((this.sda == null) ? (other.sda != null) : !this.sda.equals(other.sda)) {
            return false;
        }
        if ((this.plazuela == null) ? (other.plazuela != null) : !this.plazuela.equals(other.plazuela)) {
            return false;
        }
        if ((this.col == null) ? (other.col != null) : !this.col.equals(other.col)) {
            return false;
        }
        if ((this.res == null) ? (other.res != null) : !this.res.equals(other.res)) {
            return false;
        }
        if ((this.urb == null) ? (other.urb != null) : !this.urb.equals(other.urb)) {
            return false;
        }
        if ((this.bo == null) ? (other.bo != null) : !this.bo.equals(other.bo)) {
            return false;
        }
        if ((this.repto == null) ? (other.repto != null) : !this.repto.equals(other.repto)) {
            return false;
        }
        if ((this.cton == null) ? (other.cton != null) : !this.cton.equals(other.cton)) {
            return false;
        }
        if ((this.crio == null) ? (other.crio != null) : !this.crio.equals(other.crio)) {
            return false;
        }
        if ((this.lotif == null) ? (other.lotif != null) : !this.lotif.equals(other.lotif)) {
            return false;
        }
        if ((this.valle == null) ? (other.valle != null) : !this.valle.equals(other.valle)) {
            return false;
        }
        if ((this.com == null) ? (other.com != null) : !this.com.equals(other.com)) {
            return false;
        }
        if ((this.cond == null) ? (other.cond != null) : !this.cond.equals(other.cond)) {
            return false;
        }
        if ((this.curb == null) ? (other.curb != null) : !this.curb.equals(other.curb)) {
            return false;
        }
        if ((this.ctrocom == null) ? (other.ctrocom != null) : !this.ctrocom.equals(other.ctrocom)) {
            return false;
        }
        if ((this.aldea == null) ? (other.aldea != null) : !this.aldea.equals(other.aldea)) {
            return false;
        }
        if ((this.alameda == null) ? (other.alameda != null) : !this.alameda.equals(other.alameda)) {
            return false;
        }
        if ((this.edif == null) ? (other.edif != null) : !this.edif.equals(other.edif)) {
            return false;
        }
        if ((this.numeral == null) ? (other.numeral != null) : !this.numeral.equals(other.numeral)) {
            return false;
        }
        if ((this.apto == null) ? (other.apto != null) : !this.apto.equals(other.apto)) {
            return false;
        }
        if ((this.local == null) ? (other.local != null) : !this.local.equals(other.local)) {
            return false;
        }
        if ((this.lote == null) ? (other.lote != null) : !this.lote.equals(other.lote)) {
            return false;
        }
        return true;
    }

  
        
        
}
